package com.quanxi.qxaimain.service;

import com.alibaba.fastjson.JSONObject;
import com.quanxi.qxaimain.common.globalException.ResultCode;
import com.quanxi.qxaimain.dao.indicators.indicatorsDataMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.concurrent.Future;

import static java.lang.Thread.sleep;

/**
 * @Author lyx
 * @create 2022/9/30 15:50
 */
@Slf4j
@Service("demoserver")
public class demoImpl implements demoserver {

    @Async
    public Future springAsynchronousMethod() {
        try {
            Integer result = longTimeMethod();
            String root=  System.getProperty("User.dir")+"\\qxaimainlog\\SqlOvertime60s.txt";
            File fileOrFilename  = new File(root);
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileOrFilename, true)));
            System.currentTimeMillis();
            out.write(   System.currentTimeMillis()+"first 第一种方式 \r\n");
            out.close();
            log.info("加载成功");
            return new AsyncResult(result);
        }catch (Exception e){
            log.info(e.getMessage());
           return   new AsyncResult(1);
        }

    }

    @Resource
    indicatorsDataMapper indicatorsDataMapper;
    @Override
    @Async
    public Future sqlMongoDb(String sql) {
        try {
            JSONObject jsonObject = exec_sql(sql);

            // 结果存放MongoDB


            String root=  System.getProperty("User.dir")+"\\qxaimainlog\\SqlOvertime60s.txt";
            File fileOrFilename  = new File(root);
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileOrFilename, true)));
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            out.write(df.format(System.currentTimeMillis())+":"+jsonObject+"\r\n");
            out.close();
            log.info("加载成功");
        }
        catch (Exception e ){
            log.info(e.getMessage());
        }
        return null;
    }

    private  Integer longTimeMethod() throws InterruptedException {
        int t=10000;
        sleep(t);
        System.out.println("10000ms");
        return t;
    }



    private JSONObject exec_sql(String sql)  {
        JSONObject jsonObject = new JSONObject();
        long starttime=System.currentTimeMillis();
        try{
            List<LinkedHashMap<String, Object>> linkedHashMaps = indicatorsDataMapper.exec_sql(sql);
            jsonObject.put("sql_result", linkedHashMaps);
            jsonObject.put("sql_message", ResultCode.SQL_SUCCESS);
            jsonObject.put("sql",sql);
            if(System.currentTimeMillis()-starttime>60000){
                jsonObject.put("sql_message", ResultCode.SQL_OVER_TIME_60S);
            }
            jsonObject.put("time",System.currentTimeMillis()-starttime);
        }
        catch (Exception e){
            jsonObject.put("sql_message",ResultCode.SQL_ERROR);
            jsonObject.put("sql_message_error",e.getMessage());
            log.info(e.getMessage());
            jsonObject.put("sql_result","");
            jsonObject.put("sql",sql);
            jsonObject.put("time",System.currentTimeMillis()-starttime);
        }
        return jsonObject;
    }


}